var util = require('../../utils/util.js');
var common = require("../../utils/common.js");
Page({
  data:{
      lifeList:[{ "name": "昆山公交", "img": "/image/bus.png", "link": "/pages/smartlife/bus/bus" },
      { "name": "昆山自行车", "img": "/image/bike.png", "link": "/pages/smartlife/bike/bike" },
      { "name": "旺财记账", "img": "/image/wangcai.png", "link": "/pages/smartlife/wangcai/wangcai" }],
      swipers:[
        // { "id": 76, "title": "小程序可以看电影电视啦！", "img": "http://or7ukzqb3.bkt.clouddn.com/zhouzhuang.jpg", "link": "/pages/smartlife/tv/play_list/play_list", "content":"" },
        // { "title": "千灯", "img": "http://or7ukzqb3.bkt.clouddn.com/qiandeng.jpg", "link": null },
        // { "title": "昆山", "img": "http://or7ukzqb3.bkt.clouddn.com/kunshan.jpg", "link": null },
        // { "title": "中环", "img": "http://or7ukzqb3.bkt.clouddn.com/zhonghuan.jpg", "link": null }
      ],
      recommends: [
        // { "title": "欢乐颂全集", "img": "http://xiaozhi.iwares.cn:8080/wangcai/images/huanlesong_small.jpg", "link": "/pages/smartlife/common_recommend/common_recommend?recommendid=2&title=欢乐颂全集&img=http://xiaozhi.iwares.cn:8080/wangcai/images/huanlesong_big.jpg&content=点击下面的分享按钮，并成功分享到任意群即可收到链接的推送消息" }, { "title": "广告招商", "img": "http://xiaozhi.iwares.cn:8080/wangcai/images/business.png", "link": "13913213202" }, { "title": "", "img": "http://xiaozhi.iwares.cn:8080/wangcai/images/postalinsurance.jpg", "link": "/pages/smartlife/post_auto_insurance/post_auto_insurance" }
        ],
        todayTitles: [],
      indicatorDots: true,
      autoplay: true,
      interval: 5000,
      duration: 1000,
      imgWidth: 0,
      imgHeight:0,
      recommendImgWidth:0

  },
  onLoad: function () {
    var that = this
    // var appUsed = wx.getStorageSync('appused')
    // if (!appUsed) {
    //     // 跳转到app使用引导页面
    //     wx.navigateTo({
    //       url: '/pages/smartlife/guide/guide'
    //     })
    //     wx.setStorageSync('appused', true)
    // }
    var appUp = wx.getStorageSync('appup')
    if (!appUp) {
      wx.removeStorage({
        key: 'busline'
      })
      wx.removeStorage({
        key: 'station'
      })
      wx.setStorageSync('appup', true)
    }


    try {
      var res = wx.getSystemInfoSync()
      that.setData({
        imgWidth: res.windowWidth,
        imgHeight: res.windowWidth * 2/7,
        recommendImgWidth:res.windowWidth-20
      })
      wx.setTopBarText({
        text: '我将改变世界!'
      })
      wx.request({
        url: common.apiUrl() + 'postLifeList',
        // header: {}, // 设置请求的 header
        success: function (res) {
          console.log(res)
          if (res.data.status == 1) {
            var obj = JSON.parse(res.data.data);
            that.setData({
              lifeList: obj
            })
          }
        }
      })
    } catch (e) {
      // Do something when catch error
    }
    that.onPullDownRefresh()
  },
  onPullDownRefresh: function () {
    var that = this
    that.getSwipers()
    // that.getRecommends()
    that.getTodayTitle()
  },
  onShareAppMessage: function () {
    return {
      title: '鹿城小智：秒天、秒地、秒空气~',
      path: '/pages/smartlife/smartlife'
    }
  },
  getRecommends: function (e) {
    // 请求获取recommends列表
    var that = this
    wx.request({
      url: common.apiUrl() + 'recommendList',
      success: function (res) {
        that.setData({
          recommends: res.data.data
        })
        wx.stopPullDownRefresh()
      }
    })
  },
  getTodayTitle: function (e) {
    // 请求获取recommends列表
    var that = this
    wx.request({
      url: common.ksbbsUrl() + 'get-recommend',
      success: function (res) {
        if (res.data.ret == 0) {
          that.setData({
            todayTitles: res.data.data
          })
        }
        wx.stopPullDownRefresh()
      }
    })
  },
  getSwipers: function (e) {
    var that = this
    // 请求获取swipers列表
    wx.request({
      url: common.apiUrl() + 'swipersList',
      success: function (res) {
        that.setData({
          swipers: res.data.data
        })
      }
    })
  },
  detail: function(e){
    if(e.currentTarget.dataset.link){
      // console.log(e.currentTarget.dataset.link.indexOf("budejie/budejie"))
      if (e.currentTarget.dataset.link.indexOf("budejie/budejie") > 0 || e.currentTarget.dataset.link.indexOf("wangcai/wangcai") >0) {
      wx.switchTab({
        url: e.currentTarget
          .dataset.link,
      })
    }else{
          wx.navigateTo({
            url: e.currentTarget
      .dataset.link
          })
        }
      }else{
      wx.showToast({
        title: '暂未开放'
      })
    }
  },
  swiperItemClick: function (e) {
    var that = this
    if (isNaN(e.currentTarget.dataset.link)) {
      console.log(e.currentTarget.dataset.link)
      if (e.currentTarget.dataset.link) {
        var page = e.currentTarget.dataset.link + '?recommendid=' + that.data.swipers[e.currentTarget.dataset.index].id + '&title=' + that.data.swipers[e.currentTarget.dataset.index].title + '&img=' + that.data.swipers[e.currentTarget.dataset.index].img + '&content=' + that.data.swipers[e.currentTarget.dataset.index].content
        if (page.indexOf("budejie/budejie?") > 0) {
          wx.switchTab({
            url: '/pages/budejie/budejie',
          })
        } else {
          wx.navigateTo({
            url: page,
            success: function (res) {
              // success
            },
            fail: function () {
              // fail
            },
            complete: function () {
              // complete
            }
          })
        }
      } else {
        // wx.showToast({
        //   title: '暂未开放'
        // })
      }
    } else {
      if (e.currentTarget.dataset.link == null || e.currentTarget.dataset.link == undefined || e.currentTarget.dataset.link == '') {
        // console.log(e.currentTarget.dataset.link)
      } else {
        wx.makePhoneCall({
          phoneNumber: e.currentTarget.dataset.link,
        })
      }
    }
  },
  recommendClick: function (e) {
    var that = this
    if (isNaN(e.currentTarget.dataset.link)) {
      if (e.currentTarget.dataset.link) {
        var page = e.currentTarget.dataset.link + '?recommendid=' + that.data.swipers[e.currentTarget.dataset.index].id + '&title=' + that.data.swipers[e.currentTarget.dataset.index].title + '&img=' + that.data.swipers[e.currentTarget.dataset.index].img + '&content=' + that.data.swipers[e.currentTarget.dataset.index].content
        // console.log(page)
        wx.navigateTo({
          url: page,
          success: function (res) {
            // success
          },
          fail: function () {
            // fail
          },
          complete: function () {
            // complete
          }
        })
      } else {
        // wx.showToast({
        //   title: '暂未开放'
        // })
      }
    }else{
      if (e.currentTarget.dataset.link == null || e.currentTarget.dataset.link == undefined || e.currentTarget.dataset.link == '') {
        // console.log(e.currentTarget.dataset.link)
      }else{
        wx.makePhoneCall({
          phoneNumber: e.currentTarget.dataset.link,
        })
      }
    }
  },
  ksbbsItemClick: function (e) {
    wx.navigateTo({
      url: 'ksbbs_article/ksbbs_article?tid=' + e.currentTarget.dataset.dataid
    })
    // console.log(e.currentTarget.dataset.dataid)
  }
})